﻿using blackenter.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackenter.Tests.FakeDbContexts {
    public class FakePageDbContext : IPageDbContext {

        #region Fields

        private static readonly List<PageData> pages = new List<PageData>();

        #endregion

        #region Properties

        public List<PageData> Pages {
            get {
                return pages;
            }
        }

        #endregion

        #region Methods

        internal void Reset() {
            pages.Clear();
        }

        public bool SavePage(int pageId, string pageName, string siteCulture) {
            PageData data = this.Pages.FirstOrDefault(p => p.Id.Equals(pageId));
            if (!data.Id.Equals(pageId)) return false;
            this.Pages.Remove(data);
            data.Name = pageName;
            data.SiteCulture = siteCulture;
            this.Pages.Add(data);
            return true;
        }

        public int SaveNewPage(int siteId, string pageName, string siteCulture) {
            var data = new PageData() { Id = this.Pages.Count + 1, Name = pageName, SiteId = siteId, SiteCulture = siteCulture };
            this.Pages.Add(data);
            return data.Id;
        }

        public PageData GetPageByPageId(int pageId) {
            return this.Pages.FirstOrDefault(p => p.Id.Equals(pageId));
        }

        public PageData[] GetPagesBySiteId(int siteId, string siteCulture) {
            return this.Pages.Where(p => p.SiteId == siteId && p.SiteCulture == siteCulture).ToArray();
        }

        #endregion

    }
}
